import requests
import unittest


class A3mall(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')

    def tearDown(self) -> None:
        print('end case')

    token = None
    info = None

    # 登录
    def test_A3mall_01(self):
        login = requests.post(url='http://10.59.9.14:7003/api/public/login',
                              json={"username": "18185603008", "password": "123456"})
        print(login.json())
        A3mall.token = login.json()['data']['token']
        print(A3mall.token)
        # 提取info 进行断言
        A3mall.info = login.json()['info']
        self.assertNotEqual('no', A3mall.info)

    # 我的
    username=None
    def test_A3mall_02(self):
        info = requests.get(url='http://10.59.9.14:7003/api/ucenter/info',
                            headers={'auth-token': 'Bearer ' + A3mall.token})
        print(info.json())
        # 断言
        A3mall.username=info.json()['data']['username']
        self.assertTrue('18185603008'==A3mall.username)

    title = None

    # 秒杀列表
    def test_A3mall_03(self):
        page = requests.get(url='http://10.59.9.14:7003/api/second?page=1&type=0&sort=1')
        print(page.json())
        # 提取title 进行断言
        A3mall.title = page.json()['data']['list'][0]['title']
        self.assertTrue('测试666' == A3mall.title)

    spid = None

    # 商品详情页
    def test_A3mall_04(self):
        view = requests.get(url='http://10.59.9.14:7003/api/group/view?id=12')
        print(view.json())
        # 提取商品id
        A3mall.spid = view.json()['data']['goods']['id']
        print('商品id:', A3mall.spid)
        # 断言商品id
        self.assertEqual(12, A3mall.spid)

    goods_id = None

    # 提交订单
    def test_A3mall_05(self):
        confirm = requests.get(
            url='http://10.59.9.14:7003/api/order/confirm?id=12&type=group&sku_id=&num=1&kid=0&shipping_type=1',
            headers={'auth-token': 'Bearer ' + A3mall.token})
        print(confirm.json())
        # 提取goods_id
        A3mall.goods_id = confirm.json()['data']['item'][0]['goods_id']
        print('goods_id:', A3mall.goods_id)
        # 断言商品id
        self.assertEqual(54, A3mall.goods_id)

    msg = None

    # 创建订单
    def test_A3mall_06(self):
        create = requests.post(url='http://10.59.9.14:7003/api/order/create',
                               headers={'auth-token': 'Bearer ' + A3mall.token},
                               json={
                                   "id": A3mall.spid,
                                   "type": "group",
                                   "address_id": 56,
                                   "store_id": 1,
                                   "shipping_type": 1,
                                   "bonus_id": "0",
                                   "payment": "balance",
                                   "remarks": "",
                                   "source": 1,
                                   "url": "http://10.59.9.14:7003/wap/pages/cart/confirm?id=12&kid=0&sku_id=&num=1&type=group",
                                   "sku_id": "",
                                   "num": "1",
                                   "kid": "0"
                               })
        print(create.json())
        # 提取goods_id
        A3mall.msg = create.json()['data']['msg']
        print('msg:', A3mall.msg)
        # # 断言商品id
        self.assertFalse('支付失败' == A3mall.goods_id)


if __name__ == '__main__':
    unittest.main()
